package com.ims.core.doc.service.impl;

import java.util.List;
import com.ims.common.utils.DateUtils;
import com.ims.core.doc.domain.TSalInvoiceIssueEntry;
import com.ims.core.doc.mapper.TSalInvoiceIssueMapper;
import com.ims.core.doc.service.ITSalInvoiceIssueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ims.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ims.core.doc.domain.TSalInvoiceIssue;
/**
 * 发票Service业务层处理
 *
 * @author IMS
 * @date 2024-10-25
 */
@Service
public class TSalInvoiceIssueServiceImpl implements ITSalInvoiceIssueService
{
    @Autowired
    private TSalInvoiceIssueMapper tSalInvoiceIssueMapper;

    /**
     * 查询发票
     *
     * @param id 发票主键
     * @return 发票
     */
    @Override
    public TSalInvoiceIssue selectTSalInvoiceIssueById(Long id)
    {
        return tSalInvoiceIssueMapper.selectTSalInvoiceIssueById(id);
    }

    /**
     *
     * @param invoiceId 发票单ID
     * @return
     */
    @Override
    public TSalInvoiceIssue selectTSalInvoiceIssueByInvoiceId(String invoiceId) {
        return tSalInvoiceIssueMapper.selectTSalInvoiceIssueByInvoiceId(invoiceId);
    }

    @Override
    public TSalInvoiceIssue selectTSalInvoiceIssueByInvoiceNo(String invoiceNo) {
        return tSalInvoiceIssueMapper.selectTSalInvoiceIssueByInvoiceNo(invoiceNo);
    }

    /**
     * 查询发票列表
     *
     * @param tSalInvoiceIssue 发票
     * @return 发票
     */
    @Override
    public List<TSalInvoiceIssue> selectTSalInvoiceIssueList(TSalInvoiceIssue tSalInvoiceIssue)
    {
        return tSalInvoiceIssueMapper.selectTSalInvoiceIssueList(tSalInvoiceIssue);
    }

    /**
     * 新增发票
     *
     * @param tSalInvoiceIssue 发票
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertTSalInvoiceIssue(TSalInvoiceIssue tSalInvoiceIssue)
    {
        tSalInvoiceIssue.setCreateTime(DateUtils.getNowDate());
        int rows = tSalInvoiceIssueMapper.insertTSalInvoiceIssue(tSalInvoiceIssue);
        insertTSalInvoiceIssueEntry(tSalInvoiceIssue);
        return rows;
    }

    /**
     * 修改发票
     *
     * @param tSalInvoiceIssue 发票
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateTSalInvoiceIssue(TSalInvoiceIssue tSalInvoiceIssue)
    {
        tSalInvoiceIssueMapper.deleteTSalInvoiceIssueEntryByInvoiceId(tSalInvoiceIssue.getId());
        insertTSalInvoiceIssueEntry(tSalInvoiceIssue);
        return tSalInvoiceIssueMapper.updateTSalInvoiceIssue(tSalInvoiceIssue);
    }

    /**
     * 修改发票（不修改子表）
     *
     * @param tSalInvoiceIssue 发票
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateTSalInvoiceIssueOnly(TSalInvoiceIssue tSalInvoiceIssue)
    {
        return tSalInvoiceIssueMapper.updateTSalInvoiceIssue(tSalInvoiceIssue);
    }

    /**
     * 批量删除发票
     *
     * @param ids 需要删除的发票主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteTSalInvoiceIssueByIds(Long[] ids)
    {
        tSalInvoiceIssueMapper.deleteTSalInvoiceIssueEntryByInvoiceIds(ids);
        return tSalInvoiceIssueMapper.deleteTSalInvoiceIssueByIds(ids);
    }

    /**
     * 删除发票信息
     *
     * @param id 发票主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteTSalInvoiceIssueById(Long id)
    {
        tSalInvoiceIssueMapper.deleteTSalInvoiceIssueEntryByInvoiceId(id);
        return tSalInvoiceIssueMapper.deleteTSalInvoiceIssueById(id);
    }

    /**
     * 新增销项开票返回商品明细信息
     *
     * @param tSalInvoiceIssue 发票对象
     */
    public void insertTSalInvoiceIssueEntry(TSalInvoiceIssue tSalInvoiceIssue)
    {
        List<TSalInvoiceIssueEntry> tSalInvoiceIssueEntryList = tSalInvoiceIssue.getTSalInvoiceIssueEntryList();
        String resultId = tSalInvoiceIssue.getResultId();
        if (StringUtils.isNotNull(tSalInvoiceIssueEntryList))
        {
            List<TSalInvoiceIssueEntry> list = new ArrayList<TSalInvoiceIssueEntry>();
            for (TSalInvoiceIssueEntry tSalInvoiceIssueEntry : tSalInvoiceIssueEntryList)
            {
                tSalInvoiceIssueEntry.setResultId(resultId);
                tSalInvoiceIssueEntry.setInvoiceId(tSalInvoiceIssue.getInvoiceId());
                list.add(tSalInvoiceIssueEntry);
            }
            if (list.size() > 0)
            {
                tSalInvoiceIssueMapper.batchTSalInvoiceIssueEntry(list);
            }
        }
    }
}
